package org.stream.answerduel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stream.answerduel.entity.User;
import org.stream.answerduel.repository.UserRepository;
import org.stream.answerduel.vo.RankVo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author 郭榕
 * @date 2019/7/10 15:00
 *
 * 功能描述：排行榜业务，主要是对好友排行和世界排行的业务查询排序操作;
 */
@Service
public class RankService {

    @Autowired
    private UserRepository userRepository;

    /**
     * 功能描述:世界排行业务操作,排序操作;
     * @author 郭榕
     * @date   2019/7/11 10:14
     */
    public List<RankVo> selectWorldRank(String wechatid){

        //查询所有用户;
        List<User> userList = userRepository.findToptenUser();

        User user = userRepository.findByWechatID(wechatid);
        //新建排行榜列表集合;
        List<RankVo> worldRankVoList = new ArrayList<>();
        for(int i = 0 ; i<userList.size() ; i++ )
        {
            RankVo rankVo = new RankVo();
            rankVo.setWechatID(userList.get(i).getWechatID());
            rankVo.setNickName(userList.get(i).getNickName());
            rankVo.setScore(userList.get(i).getScore());
            rankVo.setAvatarUrl(userList.get(i).getAvatarUrl());
            worldRankVoList.add(rankVo);
        }

        //将自身数据放在集合末尾,并把排名加上属性去;
        RankVo  ownRank = new RankVo();
        for(int i = 0 ; i < worldRankVoList.size() ; i++)
        {
            worldRankVoList.get(i).setNo(i+1);
            if(worldRankVoList.get(i).getWechatID().equals(wechatid))
            {
                ownRank = worldRankVoList.get(i);
            }
        }
        if(ownRank.getWechatID()==null || ownRank.getWechatID()== "")
        {
            ownRank.setNo(0);
            ownRank.setWechatID(user.getWechatID());
            ownRank.setAvatarUrl(user.getAvatarUrl());
            ownRank.setScore(user.getScore());
            ownRank.setNickName(user.getNickName());
        }
            worldRankVoList.add(ownRank);
        return worldRankVoList;
    }

    /**
     * 功能描述:好友排行业务操作,排序操作;
     * @author 郭榕
     * @date 2019/7/11 10:16
     */
    public List<RankVo> selectFriendRank(String wechatid){

        //查询该用户所有好友
        List<User> userList =  userRepository.findFriendsByWechatId(wechatid);
        //查询自己
        User user  = userRepository.findByWechatID(wechatid);
        //新建好友排行榜集合
        List<RankVo> friendsRankVoList = new ArrayList<>();
        for(int i = 0 ; i<userList.size() ; i++ )
        {
            RankVo rankVo = new RankVo();
            rankVo.setWechatID(userList.get(i).getWechatID());
            rankVo.setNickName(userList.get(i).getNickName());
            rankVo.setScore(userList.get(i).getScore());
            rankVo.setAvatarUrl(userList.get(i).getAvatarUrl());
            friendsRankVoList.add(rankVo);
        }
        //把自己添加进去;
        RankVo rankVo = new RankVo();
        rankVo.setWechatID(user.getWechatID());
        rankVo.setNickName(user.getNickName());
        rankVo.setScore(user.getScore());
        rankVo.setAvatarUrl(user.getAvatarUrl());
        friendsRankVoList.add(rankVo);
        //从大到小排序,包括自己
        rankSort(friendsRankVoList);

        //将自身数据放在集合末尾,并把排名加上属性去;
        RankVo  ownRank = new RankVo();
        for(int i = 0 ; i < friendsRankVoList.size() ; i++)
        {
            friendsRankVoList.get(i).setNo(i+1);
            if(friendsRankVoList.get(i).getWechatID().equals(wechatid))
            {
                ownRank = friendsRankVoList.get(i);
            }
        }
        friendsRankVoList.add(ownRank);
        return friendsRankVoList;
    }


    /**
     * 功能描述: 将列表排序,并区分名次，然后重写自己的排位，在列表的第一行
     * @author 郭榕
     * @date 2019/7/11 11:44
     */
    public void rankSort (List<RankVo> rankVoList){
        Collections.sort(rankVoList, new Comparator<RankVo>() {
            @Override
            public int compare(RankVo o1, RankVo o2) {
                if(o1.getScore()<o2.getScore())
                    return 1;
                if(o1.getScore()==o2.getScore())
                    return 0;
                return -1;
            }
        });
    }
}
